package com.haixee.parkdemo.common.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 跨域
 *
 * @author GUIFENG GUO
 * 2020/3/22 9:24
 */
@Slf4j
//@WebFilter(filterName = "CORSFilter", urlPatterns = {"/*"})
@Order(value = Integer.MAX_VALUE - 500)
//@Configuration
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletResponse response = (HttpServletResponse) servletResponse;

        response.setHeader("Access-Control-Allow-Origin", "*"); // todo : cookie 跨域是否需要具体的域名
        response.setHeader("Access-Control-Allow-Credentials", "true"); //是否支持cookie跨域
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, Token, token, accept, origin, Cache-Control, X-Requested-With");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");

        filterChain.doFilter(servletRequest, servletResponse);
    }

}
